package com.bac.bacplatform;

import java.util.HashMap;
import java.util.Map;

import android.app.AlertDialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;

import com.android.volley.Response;
import com.android.volley.Response.Listener;
import com.bac.utils.Config;
import com.bac.utils.Util;

public class GasTreasureActivity extends BaseActivity implements
		OnClickListener {

	private EditText bankCardId; 			// 银行卡号
	private EditText personId; 				// 身份证号
	private EditText phoneNumber; 			// 手机号
	private EditText phoneVerify; 			// 验证码
	private EditText payPassword; 			// 支付密码
	private EditText rePassword; 			// 确认密码
	private Button saveButton; 				// 保存
	private Button verifyButton; 			// 获取验证码
	private ImageButton clearPhoneNumber; 	// 手机号码清除
	
	private String mPayPasswordString;
	
	private int totalDown = 60;				// 倒计时60秒
	private Handler mHandler= new Handler();// 全局handler

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_gas_treasure);
	}

	@Override
	public void init() {
		bankCardId = (EditText) findViewById(R.id.gas_phoneNumber);
		personId = (EditText) findViewById(R.id.gas_personid);
		phoneNumber = (EditText) findViewById(R.id.gas_phonenumber);
		phoneNumber.setText(Config.UserGeneralInfo.phone);
		phoneVerify = (EditText) findViewById(R.id.gas_phoneverify);
		payPassword = (EditText) findViewById(R.id.gas_paypassword);
		rePassword = (EditText) findViewById(R.id.gas_repassword);
		saveButton = (Button) findViewById(R.id.gas_save_btn);
		verifyButton = (Button) findViewById(R.id.gas_verify_btn);
		verifyButton.setEnabled(false);
		clearPhoneNumber = (ImageButton) findViewById(R.id.btn_phonenumber_verify);
		
		saveButton.setOnClickListener(this);
		verifyButton.setOnClickListener(this);
		clearPhoneNumber.setOnClickListener(this);
		
		
		personId.addTextChangedListener(new TextWatcher() {
			@Override
			public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
			}
			@Override
			public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
					int arg3) {
			}
			@Override
			public void afterTextChanged(Editable arg0) {
				if (phoneNumber.getText().toString().length() > 0 && personId.getText().toString().length() > 0) {
					verifyButton.setEnabled(true);
					verifyButton.setBackgroundResource(R.drawable.red_btn);
				}else{
					verifyButton.setEnabled(false);
					verifyButton.setBackgroundResource(R.drawable.red_btn1);
				}
			}
		});
		
		phoneNumber.addTextChangedListener(new TextWatcher() {
			@Override
			public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
			}
			@Override
			public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
					int arg3) {
			}
			@Override
			public void afterTextChanged(Editable arg0) {
				if(phoneNumber.getText().toString().length()>0){
					clearPhoneNumber.setVisibility(View.VISIBLE);
				}else{
					clearPhoneNumber.setVisibility(View.INVISIBLE);
				}
				
				if (phoneNumber.getText().toString().length() > 0 && personId.getText().toString().length() > 0) {
					verifyButton.setEnabled(true);
					verifyButton.setBackgroundResource(R.drawable.red_btn);
				}else{
					verifyButton.setEnabled(false);
					verifyButton.setBackgroundResource(R.drawable.red_btn1);
				}
			}
		});
	}

	@Override
	public void onClick(View view) {
		switch (view.getId()) {
		case R.id.gas_save_btn:
			
			if (checkTextNone()) {
				
				openAccount();
			}
			
			break;
			
		case R.id.gas_verify_btn:
			
			getVerifyCode();
			
			verifyButton.setEnabled(false);
			
			verifyButton.setBackgroundResource(R.drawable.red_btn1);
			
			new Thread(new ClassCut()).start();//倒计时
			
			break;
			
		case R.id.btn_phonenumber_verify:
			
			if(phoneNumber.getText().toString().length()>0){
				
				phoneNumber.setText("");
				
			}
			
			break;

		default:
			break;
		}
	}
	
	public boolean checkTextNone(){
		if (personId.getText().length() == 0) {
			showToast("请输入身份证号");
			return false;
		}else if(phoneNumber.getText().length() == 0){
			showToast("请输入手机号");
			return false;
		}else if (phoneVerify.getText().length() == 0) {
			showToast("请输入验证码");
			return false;
		}else if(payPassword.getText().length() == 0){
			showToast("请输入支付密码");
			return false;
		}else if(rePassword.getText().length() == 0){
			showToast("请确认支付密码");
			return false;
		}else if (payPassword.getText().toString().equals(rePassword.getText().toString())) {
			mPayPasswordString = payPassword.getText().toString();
			return true;
		}else{
			showToast("两次密码不一致");
			return false;
		}
	}
	
	/**
	 * 获取短信验证码
	 * @return 短信验证码
	 */
	public void getVerifyCode(){
		/**
		 * 成功返回结果
		 */
		Listener<String> sL = new Response.Listener<String>() {
			@Override
			public void onResponse(String response) {
				dismissLoadingProgress();
				Log.i("GasTreasureActivity----", "success");
				Log.i(getClass().getName(), response.toString());
				
			}
		};

		showLoadingProgress();
		String path = Config.URL_RECORD_CODE;
		Map<String, String> map = new HashMap<String, String>();
		map.put("phone", phoneNumber.getText().toString());
		Util.httpRequestToGetWithParams(path, sL, this, map);
	}
	
	/**
	 * 开户，开通聚优金
	 */
	public void openAccount(){
		/**
		 * 成功返回结果
		 */
		Listener<String> sL = new Response.Listener<String>() {
			@Override
			public void onResponse(String response) {
				dismissLoadingProgress();
				Log.i("TAG----success", "开通成功"+response.toString());
				AlertDialog.Builder builder = new AlertDialog.Builder(GasTreasureActivity.this, R.style.Custom_dialog);
				final AlertDialog alertDialog = builder.create();
				alertDialog.setCanceledOnTouchOutside(false);
				alertDialog.show();
				Window window = alertDialog.getWindow();
				View view = LayoutInflater.from(getApplicationContext()).inflate(R.layout.dialog_alert, null);
				view.findViewById(R.id.ly_cancel).setVisibility(View.VISIBLE);
				view.findViewById(R.id.line).setVisibility(View.VISIBLE);
				
				((TextView)(view.findViewById(R.id.tv_title))).setText("提示");
				TextView messageTV = (TextView)view.findViewById(R.id.tv_message);
				messageTV.setText("开通成功");
				
				view.findViewById(R.id.ly_cancel).setOnClickListener(new OnClickListener() {
					@Override
					public void onClick(View v) {
						alertDialog.dismiss();
					}
				});
				
				view.findViewById(R.id.ly_ok).setOnClickListener(new OnClickListener() {
					@Override
					public void onClick(View v) {
						alertDialog.dismiss();
						Intent intent = new Intent(GasTreasureActivity.this,
								MainActivity.class);
						intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
						startActivity(intent);
					}
				});
				window.setContentView(view);
				window.setLayout((int)(Util.getWidth(getApplicationContext()) * 0.8), WindowManager.LayoutParams.WRAP_CONTENT);
			}
		};
		showLoadingProgress();
		String path = Config.URL_RECORD_CODE;
		Map<String, String> map = new HashMap<String, String>();
		map.put("idCardNo", personId.getText().toString());
		map.put("code", phoneVerify.getText().toString());
		map.put("paymentPassword", mPayPasswordString);
		Util.httpStringPost(path, sL, this, map);
	}
	
	
	class ClassCut implements Runnable {
		@Override
		public void run() {
			while (totalDown > 0) {
				totalDown--;
				mHandler.post(new Runnable() {
					@Override
					public void run() {
						verifyButton.setText("剩余:" + totalDown + "\"");
					}
				});
				try {
					Thread.sleep(1000);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
			mHandler.post(new Runnable() {
				@Override
				public void run() {
					verifyButton.setText(R.string.regist_helpverify);
					verifyButton.setEnabled(true);
					verifyButton.setBackgroundResource(R.drawable.red_btn);
				}
			});
			totalDown = 60;
		}
	}

}
